package com.bwhx.eruler.controller.casdilly;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.casdilly.SizeRuleXK;
import com.bwhx.eruler.controller.BaseController;
import com.bwhx.eruler.service.casdilly.SizeRuleXKService;

@Controller
@RequestMapping("/sizerule_xk")
public class SizeRulerXKController extends BaseController {
	
	@Autowired
	SizeRuleXKService sizeRuleXKService;

	
	
	@ResponseBody
	@RequestMapping(value = "/page.list.ashx")
	 public Map<String,Object> sizeRulerList(Integer page,Integer rows,SizeRuleXK sizeRuleXK) {
		

		
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,sizeRuleXK);
		Map<String,Object> resultMap = new HashMap<String,Object>();
		try{
			List<SizeRuleXK> list = sizeRuleXKService.list(p);
			Integer count = sizeRuleXKService.count(sizeRuleXK);

					
			resultMap.put("total",count );//total键 存放总记录数，必须的  
			resultMap.put("rows",list);//rows键 存放每页记录 list 
			
		
			
		}catch(Exception e){
			log.error(getTrace(e));
			resultMap.put("msg", "失败！");
			resultMap.put("status", "0");
		}
		
	    return resultMap;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdate.ashx")
	public String saveOrUpdate(SizeRuleXK sizeRuleXK) {
		try{
			
			Staff sessionUser = this.getSessionStaff();
			
			
			if(sizeRuleXK.getsRXKID().equals("-1")){
				sizeRuleXK.setCreatorID(sessionUser.getsId().toString());
				sizeRuleXK.setCreateTime(new Date());
				SizeRuleXK sr = sizeRuleXKService.findConflict(sizeRuleXK);
				
				if(sr==null){
					sizeRuleXKService.insert(sizeRuleXK);
				}else{
					return "conflict";  
				}
			
				
				
			}else{
				
				sizeRuleXK.setModifierID (sessionUser.getsId().toString());
				sizeRuleXK.setModifyTime(new Date());
				
			
				SizeRuleXK sr = sizeRuleXKService.findConflict(sizeRuleXK);
				
				if(sr==null){
					sizeRuleXKService.update(sizeRuleXK);
				}else{
					return "conflict";  
				}

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.del.ashx")
	public String del(SizeRuleXK sizeRuleXK) {
		try{
			
			Staff sessionUser = this.getSessionStaff();	
			sizeRuleXK.setModifierID (sessionUser.getsId().toString());
			sizeRuleXK.setModifyTime(new Date());
			sizeRuleXKService.delete(sizeRuleXK);
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }

}
